package com.ruoyi.cpi.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.cpi.domain.Cpi;
import com.ruoyi.cpi.service.ICpiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * CPI导入Controller
 *
 * @author ruoyi
 * @date 2023-06-30
 */
@RestController
@RequestMapping("/cpi/cpi")
public class CpiController extends BaseController
{
    @Autowired
    private ICpiService cpiService;

    /**
     * 查询CPI导入列表
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:list')")
    @GetMapping("/list")
    public TableDataInfo list(Cpi cpi)
    {
        startPage();
        List<Cpi> list = cpiService.list(cpi);
        return getDataTable(list);
    }

    /**
     * 条件查询全国CPI，导入列表
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:list')")
    @GetMapping("/getNationCpi")
    public TableDataInfo getNationCpi(Cpi cpi)
    {
        startPage();
        List<Cpi> list = cpiService.getNationCpi(cpi);
        return getDataTable(list);
    }


    /**
     * 条件查询CPI，导入列表
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:getList')")
    @GetMapping("/getList")
    public TableDataInfo getList(String mname)
    {
        startPage();
        List<Cpi> list = cpiService.getList(mname);
        return getDataTable(list);
    }

    /**
     * 导出CPI导入列表
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:export')")
    @Log(title = "CPI导入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Cpi cpi)
    {
        List<Cpi> list = cpiService.list(cpi);

        ExcelUtil<Cpi> util = new ExcelUtil<Cpi>(Cpi.class);
        util.exportExcel(response, list, "CPI导入数据");
    }

    /**
     * 获取CPI导入详细信息
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:query')")
    @GetMapping(value = "/{CPIid}")
    public AjaxResult getInfo(@PathVariable("CPIid") Long CPIid)
    {
        return success(cpiService.getById(CPIid));
    }

    /**
     * 新增CPI导入
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:add')")
    @Log(title = "CPI导入", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Cpi cpi)
    {
        cpi.setCreatetime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(cpiService.save(cpi));
    }

    /**
     * 修改CPI导入
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:edit')")
    @Log(title = "CPI导入", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Cpi cpi)
    {
        cpi.setModifytime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        return toAjax(cpiService.updateById(cpi));
    }

    /**
     * 删除CPI导入
     */
    @PreAuthorize("@ss.hasPermi('cpi:cpi:remove')")
    @Log(title = "CPI导入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{CPIids}")
    public AjaxResult remove(@PathVariable List<Long> CPIids)
    {
        return toAjax(cpiService.removeByIds(CPIids));
    }
}
